package com.pengcheng.test;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
@Slf4j
public class Tcpclienthandler extends ChannelInboundHandlerAdapter {
    @Autowired
    public Common common;


    public static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        super.channelRead(ctx, msg);
        byte[] message=(byte[])msg;
        log.info("channelread:"+common.byte2hex(message));
        String content=common.byte2hex(message);
        if(content.equals("05030024000A8442"))
        {
            String response="050314746573743030303030303030303030";
            String index=(10000+ChannelCache.find(ctx.channel()))+"";
            log.info("index:"+index);
            for(int i=0;i<index.length();i++)
            {
                String str=Integer.toHexString(((int)index.charAt(i)));
                response+=str;
            }
            byte[] old=common.hex2byte(response);
            byte[] data=new byte[old.length+2];
            int crc=common.calculate(old);
            for(int i=0;i<old.length;i++)
            {
                data[i]=old[i];
            }
            byte crcHigh = (byte) ((crc >> 8) & 0xFF);
            byte crcLow = (byte) (crc & 0xFF);
            data[data.length-2]=crcLow;
            data[data.length-1]=crcHigh;
            ByteBuf bb = Unpooled.buffer(data.length);
            bb.writeBytes(data);
            ctx.writeAndFlush(bb);
            log.info(common.byte2hex(data));
        }
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        log.info("active");
        channelGroup.add(ctx.channel());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        log.info("inactive");
        remove_handle(ctx.channel());
    }
    private void remove_handle(Channel ctx)
    {
        log.info("remove_handler:"+ctx.id());
        channelGroup.remove(ctx);
    }
    protected void run(){
        String response="746573745F6865617274";
        log.info(common.hexToASCII(response));
        byte[] data=common.hex2byte(response);
        ByteBuf bb = Unpooled.buffer(data.length);
        bb.writeBytes(data);
        channelGroup.forEach(channel -> {
            bb.writeBytes(data);
            channel.writeAndFlush(bb);
        });
    }
    protected void upload(){
        channelGroup.forEach(channel -> {
            byte[] data=common.get_update();
            log.info(channel.id()+":"+common.byte2hex(data));
            ByteBuf bb = Unpooled.buffer(data.length);
            bb.writeBytes(data);
            channel.writeAndFlush(bb);
        });
    }
}
